<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2125 – Brace expansions and globs are literal in assignments. Quote it or use an array.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2125 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2125">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="brace-expansions-and-globs-are-literal-in-assignments-quote-it-or-use-an-array">Brace
expansions and globs are literal in assignments. Quote it or use an
array.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2125.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="dt">{</span><span class="dv">1</span><span class="dt">..</span><span class="dv">9</span><span class="dt">}</span></span>
<span id="cb1-2"><a href="SC2125.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$foo</span><span class="st">&quot;</span></span></code></pre></div>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2125.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="st">&quot;/some/path/*&quot;</span></span>
<span id="cb2-2"><a href="SC2125.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$foo</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2125.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="va">(</span> <span class="dt">{</span><span class="dv">1</span><span class="dt">..</span><span class="dv">9</span><span class="dt">}</span> <span class="va">)</span></span>
<span id="cb3-2"><a href="SC2125.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">${foo</span><span class="op">[@]</span><span class="va">}</span><span class="st">&quot;</span></span></code></pre></div>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2125.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="va">(</span>/some/path/<span class="pp">*</span><span class="va">)</span></span>
<span id="cb4-2"><a href="SC2125.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">${foo</span><span class="op">[@]</span><span class="va">}</span><span class="st">&quot;</span></span></code></pre></div>
<p>Note that either of these will trigger SC3030 ("In POSIX sh, array
references are undefined") if you are using <code>sh</code> and not e.g.
<code>bash</code>.</p>
<h3 id="rationale">Rationale:</h3>
<p><code>echo *.png {1..9}</code> expands to all png files and numbers
from 1 to 9, but <code>var=*.png</code> or <code>var={1..9}</code> will
just assign the literal strings <code>'*.png'</code> and
<code>'{1..9}'</code>.</p>
<p>To make the variable contain all png files or 1 through 9, use an
array as demonstrated.</p>
<p>If you intended to assign these values as literals, quote them (e.g.
<code>var="*.png"</code>).</p>
<h3 id="exceptions">Exceptions</h3>
<p>None.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


